package lab_3;

import org.omg.CORBA.*;
import org.omg.CosNaming.*;
import org.omg.PortableServer.POA;
import java.net.InetAddress;

/*
 *
 * @author grp02 - Pascal Charreix and Meike Paetz
 *
 * This is the hotel operator server implementation.
 */
public class HotelOperatorServer {

    private final String contContext = "grp02";
    private HotelOperatorContingent contRef;
    private ORB myORB;
    private NamingContextExt rootNC;
    /*
     *
     * This is the main.
     */

    public static void main(String[] args) {
        HotelOperatorServer server = new HotelOperatorServer(args);
        server.createContingent();
        server.createNamingContext();
        server.registerContingents();
    }
    /*
     *
     * This is the constructor
     */

    public HotelOperatorServer(String[] args) {
        // Initialize ORB reference
        myORB = ORB.init(args, null);
    }
    /*
     *
     * This is to create contingent
     */

    private void createContingent() {
        try {
            // Get the root Portable Object Adapter (POA)
            POA rootPOA = (POA) myORB.resolve_initial_references("RootPOA");
            // Activate the POA manager
            rootPOA.the_POAManager().activate();
            // Create instances of an Account servant (interface
            // implementation) and register them with the ORB
            HotelOperatorContingentServant contingent = new HotelOperatorContingentServant(42, "France", "Accor", 12, 827, 1234);
            contingent.setORB(myORB);
            // Activate our servants, so that direct requests (through
            // a corbaname or IOR URL) will work immediately
            rootPOA.activate_object(contingent);
            // Get object references from the implementations
            org.omg.CORBA.Object objRef = rootPOA.servant_to_reference(contingent);
            contRef = HotelOperatorContingentHelper.narrow(objRef);
        } catch (org.omg.CORBA.ORBPackage.InvalidName invN) {
            System.out.println("Couldn't locate a RootPOA");
        } catch (Exception e) {
            System.out.println("An error occurred while initializing server object:");
            e.printStackTrace();
        }
    }
    /*
     *
     * This is to create the naming context.
     */

    private void createNamingContext() {
        try {
            // Get the root name context (use the INS interface, so that we
            // can use the simpler name construction process)
            org.omg.CORBA.Object objRef = myORB.resolve_initial_references("NameService");
            rootNC = NamingContextExtHelper.narrow(objRef);
            // Create a new context
            NamingContext ctx = rootNC.new_context();
            // Construct the name URL for the new context, and convert it to a
            // Name (a NameComponent array) using the INS utility method to_name()
            NameComponent path[] = rootNC.to_name(contContext);
            // Bind the context to the name
            rootNC.rebind_context(path, ctx);
            System.out.println("Bound new context to " + contContext);
        } catch (org.omg.CORBA.ORBPackage.InvalidName invN) {
            System.out.println("Couldn't locate a Naming Service");
        } catch (org.omg.CosNaming.NamingContextPackage.InvalidName invN) {
            System.out.println("Invalid context name");
        } catch (Exception e) {
            System.out.println("An error occurred while initializing server object:");
        }
    }
    /*
     *
     * This is to register contingents
     */

    private void registerContingents() {
        try {
            // Register the objects with the Name Service.
            // Use the Interoperable Naming Service interface to simplify
            // matters, and to support URL-formatted names (e.g. "JohnS",
            // "corbaname://orbhost.com:1234#JohnS", etc.)
            NameComponent[] name = rootNC.to_name(contContext + "/" + "contingent");
            rootNC.rebind(name, contRef);
            System.out.println("Registered contingent under name " + "contingent");
            // Only report the results for the 1st object
            System.out.println("New contingent created and registered. " + "URLs for the contingent are: ");
            // Get the IOR using object_to_string()
            System.out.println("\nIOR");
            System.out.println("\t" + myORB.object_to_string(contRef));
            System.out.println("\ncorbaname");
            // Construct the corbaname URL from our ORB and
            // Naming Service parameters.
            System.out.println("\tcorbaname:iiop:" + InetAddress.getLocalHost().getHostName() + ":1234#" + contContext + "/" + "contingent");
            System.out.println("\nHotelOperatorServer ready and waiting ...");
            // Go into a wait state, waiting for clients to connect
            myORB.run();
        } catch (org.omg.CosNaming.NamingContextPackage.InvalidName invN) {
            System.out.println("Invalid object name");
        } catch (Exception e) {
            System.out.println("An error occurred while initializing server object:");
        }
    }
}
